package com.gm.service.user;

import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.gm.common.Pager;
import com.gm.dao.right.IRightDao;
import com.gm.dao.role.IRoleDao;
import com.gm.dao.user.IUserDao;
import com.gm.model.po.User;
import com.gm.tools.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by yangxing on 2017/6/8.
 */
@Service
public class UserService {
    @Autowired
    private IUserDao userDao;
    @Autowired
    private IRoleDao roleDao;
    @Autowired
    private IRightDao rightDao;
    private final Logger log = LoggerFactory.getLogger(this.getClass());
    /**
     * 管理员登录
     * @param username 用户名
     * @param password 密码
     * @return
     */
    public User userLogin(String username,String password){
        User user = getUserByUserName(username);
        return user;
    }


    public User getUserByUserName(String username){
        return  userDao.selectByUserName(username);
    }

    /**
     * 添加用户
     * @param user
     * @return
     */
    public int addUser(User user){
        user.setStatus(2);
        user.setCreateTime(DateUtils.getCurrentUnixTime());
        user.setUpdateTime(DateUtils.getCurrentUnixTime());
        return  userDao.insert(user);
    }

    /**
     * 修改用户
     * @param user
     * @return
     */
    public int updateUser(User user){
        User u = userDao.selectByPrimaryKey(user.getId());
        u.setUpdateTime(DateUtils.getCurrentUnixTime());
        u.setRoles(user.getRoles());
        u.setName(user.getName());
        u.setPhoneNum(user.getPhoneNum());
        u.setEmail(user.getEmail());
        return  userDao.updateByPrimaryKey(u);
    }


    public List getUsereList(Pager pager){
        PageBounds pageBounds = new PageBounds(pager.getCurrentPage(), pager.getPageSize() );
        return userDao.getUserList(pageBounds);


    }
    public User selectByPrimaryKey(Integer userId){
        return userDao.selectByPrimaryKey(userId);
    }


    public int deleteUser(Integer userId){
        User user = userDao.selectByPrimaryKey(userId);
        user.setStatus(0);
        return userDao.updateByPrimaryKey(user);
    }
}